<?php
class CommentsController extends AppController {

    var $name = 'Comments';
    var $components = array('Json');
    var $helpers = array('Javascript');

    function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('add','addComment');
    }

    /**
     * adding comment where javascript disable on Post view page
     * usage in : /posts/view.ctp
     */
    function add(){

    }

    /**
     * Get status of incoming comments
     * usage in : layout/admin.ctp
     */
    function status(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        $count = $this->Comment->countIncomingComments();
        $this->Json->show($count);
    }

    /**
     * Approved incoming comments
     * usage in : comments/manage.ctp
     */
    function approve(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        App::import('Sanitize');
        
        /* Defining tags array variables from ajax input */
        $comment_id = Sanitize::paranoid($this->params['form']['comment']);
        if(!empty($comment_id)){
            $this->Comment->id = $comment_id;
            $this->Comment->saveField('status','1');
            $this->Json->result('1');
        }else{
            $this->Json->error('0');
        }
    }

    /**
     * Approved incoming comments
     * usage in : comments/manage.ctp
     */
    function delete(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        App::import('Sanitize');

        /* Defining tags array variables from ajax input */
        $comment_id = Sanitize::paranoid($this->params['form']['comment']);
        if(!empty($comment_id)){
            $post_id = $this->Comment->getPostIdByComment($comment_id);
            if(!empty($post_id)){
                $this->Comment->del($comment_id);
                $this->Comment->Post->updateAll(array('Post.comment_count'=>"Post.comment_count-1"),array('Post.post_id'=>$post_id));
                $this->Json->result('1');
            }else{
               $this->Json->error('0');
            }
        }else{
                $this->Json->error('0');
        }
    }

    /**
     * Managing comments
     */
    function manage(){
        $this->layout = 'admin';
        $this->paginate = $this->Comment->getAllComments();
        $this->Comment->updateAll(
            array('Comment.read' => "'0'"),
            array('Comment.read' => 1)
        );

        $this->set('comments', $this->paginate('Comment'));
    }

    /**
     * Adding comment from ajax request
     * usage in : /posts/view.ctp
     */
    function addComment(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        App::import('Sanitize');

        /* Defining tags array variables from ajax input */
        $name = Sanitize::escape($this->params['form']['name']);
        $email = Sanitize::escape($this->params['form']['email']);
        $website = Sanitize::escape($this->params['form']['website']);
        $description = Sanitize::html($this->params['form']['description']);
        $slug = Sanitize::escape($this->params['form']['post']);

        if(!empty($name) && !empty($email) && !empty($description) && !empty($slug)){
            $getPost = $this->Comment->Post->checkPostBySlug($slug);
            $post_id = $getPost['Post']['post_id'];
            if(!empty($post_id)){

                 /* Processing comment description */
                $paragraph = explode("\n\n",$description);
                foreach($paragraph as $value=>&$key){
                    $filterKey = '<p>'.$key.'</p>';
                    $key = nl2br($filterKey);
                }
                $comment = implode(" ",$paragraph);

                $this->data['Comment']['name']=$name;
                $this->data['Comment']['email']=$email;
                $this->data['Comment']['website']=$name;
                $this->data['Comment']['description']=$comment;
                $this->data['Comment']['post_id']=$post_id;
                $this->data['Comment']['created']=date('Y-m-d H:i:s');

                $this->Comment->set($this->data);
                if($this->Comment->validates()){
                   /* Checking same comment by same user in 5 minutes below */
                   $commentHistory = $this->Comment->checkCommentHistory($comment,$post_id,$email);
                   if(!empty($commentHistory)){ $this->Json->error('6'); }
                   $permission = $this->Comment->Visitor->checkVisitor($email);
                    if(!empty($permission)){
                        if($permission['Visitor']['status'] == '1'){
                            $this->Comment->create();
                            $this->data['Comment']['status']='1';
                            if($this->Comment->save($this->data)){
                                $result = array('result'=>'2','email'=>md5($email));
                                $this->Json->show($result);
                            }else{
                                $this->Json->error('4');
                            }
                        }else{
                            $this->Json->error('5');
                        }
                    }else{
                        $this->Comment->create();                        
                        if($this->Comment->save($this->data)){
                            $result = array('result'=>'1','email'=>md5($email));
                            $this->Json->show($result);
                        }else{
                            $this->Json->error('4');
                        }
                    }
                }else{
                    $error = $this->Comment->invalidFields();
                    $this->Json->error($error);
                }
            }else{
                $this->Json->error('1');
            }
        }else{
            $this->Json->error('0');
        }
    }

}
?>